﻿using Cocbook_DAL;
using Cocbook_DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sale_and_manage
{
    public partial class frmAddStaff : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmAddStaff()
        {
            InitializeComponent();
        }

        private void btnLogup_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                highlighter1.SetHighlightColor(txtName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Chưa nhập tên";
                txtName.Focus();
            }
            else
            {
                if (txtUserName.Text == "")
                {
                    highlighter1.SetHighlightColor(txtUserName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    lblValidate.Text = "Chưa nhập tên đăng nhập";
                    txtUserName.Focus();
                }
                else
                {
                    if ((new StaffDAL()).GetStaffByUserName(txtUserName.Text) != null)
                    {
                        highlighter1.SetHighlightColor(txtUserName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                        lblValidate.Text = "Tên đăng nhập đã được sử dụng.";
                        txtUserName.Focus();
                        txtUserName.SelectAll();
                    }
                    else
                    {
                        if (txtPassword.Text == "")
                        {
                            highlighter1.SetHighlightColor(txtPassword, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                            lblValidate.Text = "Chưa nhập mật khẩu.";
                            txtPassword.Focus();
                        }
                        else
                        {
                            if (txtPassword2.Text != txtPassword.Text)
                            {
                                txtPassword2_Leave(null, null);
                            }
                            else
                            {
                                if (cboPosition.SelectedIndex == -1)
                                {
                                    highlighter1.SetHighlightColor(cboPosition, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                                    lblValidate.Text = "Chưa chọn quyền hạn.";
                                }
                                else
                                {
                                    string position = "";
                                    if (cboPosition.SelectedIndex == 0)
                                    {
                                        position = "seller";
                                    }
                                    else
                                    {
                                        position = "staff";
                                    }
                                    (new StaffDAL()).CreateStaff(new Staff(0, txtName.Text, txtUserName.Text, txtPassword.Text, position));
                                    ClearAll();
                                    MessageBox.Show("Thêm thành công");
                                }
                            }
                        }
                    }
                }
            }
        }

        public void ClearAll()
        {
            txtName.Text = "";
            txtPassword.Text = "";
            txtPassword2.Text = "";
            txtUserName.Text = "";
            cboPosition.Text = "";
            txtName.Focus();
        }

        private void txtPassword2_Leave(object sender, EventArgs e)
        {
            if (txtPassword.Text != txtPassword2.Text)
            {
                highlighter1.SetHighlightColor(txtPassword2, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Sai mật khẩu.";
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtUserName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtPassword, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtPassword2_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtPassword2, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void cboPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(cboPosition, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearAll();
        }


    }
}
